<?php
class Category_model extends CI_Model{
	function Category_model(){
		parent::__construct();
	}
	
	/**
	 * Get All category with category_id_ref equals to NULL
	 */
	public function getAll($lang = 'vi'){
		$data = array();
		$this->db->select('c.id, c.image, cl.category_name, c.default');
		$this->db->from('categories c');
		$this->db->where('c.public','1');
		$this->db->where('c.category_id_ref',NULL);
		$this->db->order_by('c.order');
		$this->db->join('category_languages cl','c.id = cl.category_id');
		$this->db->join('languages l','l.id = cl.language_id AND l.code =\''.$lang. '\'');
		
		$q = $this->db->get();
		
		if($q->num_rows()){
			foreach ($q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$q->free_result();
		
		return $data;
	}
	
	/**
	 * 
	 * Get all chilren of category by default category id
	 * @param unknown_type $id
	 * @param unknown_type $lang
	 */
	public function getCategoryByDefaultCatId($id,$lang){
		$data = array();
		
		$this->db->select('c.id, c.image, cl.category_name');
		$this->db->from('categories c');
		$this->db->where('c.public','1');
		$this->db->where('c.category_id_ref',$id);
		$this->db->order_by('c.order');
		$this->db->join('category_languages cl','c.id = cl.category_id');
		$this->db->join('languages l','l.id = cl.language_id AND l.code =\''.$lang. '\'');
		
		
		$q = $this->db->get();
		
		if($q->num_rows()){
			$data = $q->result_array();
		}
		
		$q->free_result();
		
		return $data;
	}
	
}